package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.UserDao;
import com.daoimpl.UserDaoImpl;
import com.vo.User;

/**
 * Servlet implementation class FindUserServlet
 */
@WebServlet("/FindUserServlet")
public class FindUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setHeader("content-type", "text/html; charset=UTF-8");
		
	    User user=new User();
	    user.setUsername(request.getParameter("username"));
	    user.setPassword(request.getParameter("password"));
	    user.setType(request.getParameter("type"));
	    
	    UserDao dao=new UserDaoImpl();
	    User userinfo=dao.findUser(user);
	    if(userinfo != null) {
	    	HttpSession session=request.getSession();
		    session.setAttribute("userinfo", userinfo);
		    request.getRequestDispatcher("index.jsp").forward(request, response);
	    }else {
	    	response.getWriter().print("用户不存在或账户密码错误，请重新登录！");
	    	response.setHeader("refresh","0.5; URL=index.jsp");
	    }
	    
	    
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
